/**
 * 
 */
package com.alstudio.ui.module.voip;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.alstudio.app.ALLocalEnv;
import com.alstudio.module.xmpp.protocol.xmpp.packact.ALMessage;
import com.alstudio.pojo.module.user.User;
import com.alstudio.utils.activity.ALActivityManager;
import com.alstudio.utils.android.color.ALColorUtils;
import com.alstudio.utils.formater.sex.ALSexFormatter;
import com.alstudio.utils.formater.time.ALTimeUtils;
import com.alstudio.view.absadapter.ViewAdapter;
import com.loovee.imaohu.R;

/**
 * @author alonso lee
 * 
 */
public class CallHistoryAdapter extends ViewAdapter {

	private ArrayList<ALMessage> data = new ArrayList<ALMessage>();

	private Context context;

	private boolean showDelete;

	public interface AddOnIconClickListener {
		public void onAddOnIconClick(Object obj);
	}

	private AddOnIconClickListener addOnIconClickListener;

	private boolean showDeleteBtnAnim;

	/**
	 * 
	 */
	public CallHistoryAdapter(Context context, ArrayList<ALMessage> data) {
		// TODO Auto-generated constructor stub
		this.data = data;
		this.context = context;
	}

	/**
	 * @param addOnIconClickListener
	 *            the addOnIconClickListener to set
	 */
	public void setAddOnIconClickListener(
			AddOnIconClickListener addOnIconClickListener) {
		this.addOnIconClickListener = addOnIconClickListener;
	}

	/**
	 * @param showDelete
	 *            the showDelete to set
	 */
	public void setShowDelete(boolean showDelete) {
		this.showDelete = showDelete;
	}

	/**
	 * @param showDeleteBtnAnim
	 *            the showDeleteBtnAnim to set
	 */
	public void setShowDeleteBtnAnim(boolean showDeleteBtnAnim) {
		this.showDeleteBtnAnim = showDeleteBtnAnim;
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		final ViewHolder holder;
		if (convertView == null) {
			int reslayout = R.layout.call_histroy_item;
			view = ALLocalEnv.getLocalEnv().getGlobalLayoutInflater()
					.inflate(reslayout, parent, false);

			holder = new ViewHolder();
			holder.delete_bt = (TextView) view.findViewById(R.id.delete_bt);
			holder.time = (TextView) view.findViewById(R.id.time);
			holder.nick_name = (TextView) view.findViewById(R.id.nick_name);
			holder.call_direction = (ImageView) view
					.findViewById(R.id.call_direction);
			holder.sex = (ImageView) view.findViewById(R.id.sex);
			holder.vauth = (ImageView) view.findViewById(R.id.vauth);

			holder.right_layout = view.findViewById(R.id.right_layout);
			holder.main_layout = view.findViewById(R.id.main_layout);

			holder.image = (ImageView) view.findViewById(R.id.icon);
			holder.call_state = (TextView) view.findViewById(R.id.call_state);
			holder.addon_icon = (ImageView) view.findViewById(R.id.addon_icon);

			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}

		setupViewItem(holder, position);
		return view;
	}

	private void setupViewItem(ViewHolder holder, int pos) {
		ALMessage history = data.get(pos);
		User u = (User) history.getData();
		holder.nick_name.setText(u.getNick());

		ALLocalEnv.getLocalEnv().displayImagePluginServerlet(
				u.getImageFileId(), holder.image,
				ALLocalEnv.getLocalEnv().getRoundImageDisplayOption());

		holder.time.setText(ALTimeUtils.formartVoipStartTime(context, history
				.getDate().getTime()));
		holder.call_direction.setVisibility(View.GONE);

		// 呼入呼出
		String txt = ALLocalEnv.getLocalEnv().getString(
				R.string.TxtCallOutgoing);
		if (u.isCaller()) {
			holder.call_direction.setVisibility(View.VISIBLE);
		}

		holder.sex.setImageResource(ALSexFormatter.transSexBigDrawable(u
				.getSex()));
		if (u.isVauthed()) {
			holder.vauth.setImageResource(R.drawable.find_user_icon_vip);
		} else {
			holder.vauth.setImageBitmap(null);
		}

		holder.nick_name.setTextColor(ALColorUtils.getColor(
				ALLocalEnv.getLocalEnv(), R.color.text_color_default));

		holder.call_state.setTextColor(ALColorUtils.getColor(
				ALLocalEnv.getLocalEnv(), R.color.call_history_calling_time));
		// 通話狀態
		switch (u.getCallRecordType()) {
		case User.CALL_STATE_CANCEL:
			txt = ALLocalEnv.getLocalEnv().getString(R.string.TxtCallCancel);
			break;
		case User.CALL_STATE_MISS:
			txt = ALLocalEnv.getLocalEnv().getString(R.string.TxtMissCall);
			holder.nick_name.setTextColor(ALColorUtils.getColor(
					ALLocalEnv.getLocalEnv(), R.color.call_history_red));
			break;
		case User.CALL_STATE_ACCEPT:
			txt = ALLocalEnv.getLocalEnv().getString(R.string.TxtCallTime);
			txt = String.format(
					txt,
					ALTimeUtils.formatVoipCallingTime(context,
							history.getCallTime()));
			break;
		case User.CALL_STATE_REJECT:
			txt = ALLocalEnv.getLocalEnv().getString(R.string.BtnReject);
			break;
		case User.CALL_STATE_CALL_NOTIFY:
			txt = ALLocalEnv.getLocalEnv().getString(R.string.TxtMissCall);
			txt += "("
					+ ALLocalEnv.getLocalEnv()
							.getString(R.string.TxtCallNotice) + ")";
			holder.nick_name.setTextColor(ALColorUtils.getColor(
					ALLocalEnv.getLocalEnv(), R.color.call_history_red));
			break;
		}
		holder.call_state.setText(txt);

		if (showDelete) {
			holder.delete_bt.setVisibility(View.VISIBLE);
			if (showDeleteBtnAnim) {
				holder.delete_bt
						.startAnimation(AnimationUtils.loadAnimation(
								ALLocalEnv.getLocalEnv(),
								R.anim.activity_left_to_right));

				holder.main_layout
						.startAnimation(AnimationUtils.loadAnimation(
								ALLocalEnv.getLocalEnv(),
								R.anim.activity_left_to_right));
			}
			holder.right_layout.setVisibility(View.GONE);
			setupAddOnIconClickEvent(holder.delete_bt, pos);
		} else {
			
//			if (showDeleteBtnAnim) {
//				holder.delete_bt
//						.startAnimation(AnimationUtils.loadAnimation(
//								ALLocalEnv.getLocalEnv(),
//								R.anim.activity_left_to_right));
//
//				holder.main_layout
//						.startAnimation(AnimationUtils.loadAnimation(
//								ALLocalEnv.getLocalEnv(),
//								R.anim.activity_left_to_right));
//			}
			
			holder.delete_bt.setVisibility(View.GONE);
			holder.right_layout.setVisibility(View.VISIBLE);
			holder.delete_bt.setOnClickListener(null);
		}

		setAddOnEvent(holder, u);

	}

	private void setAddOnEvent(ViewHolder holder, final User u) {
		holder.addon_icon.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent i = new Intent(ALActivityManager.getALActivityManager()
						.getCurrentActivity(), CallDetailActivity.class);
				i.putExtra("user", u);
				ALActivityManager.getALActivityManager().getCurrentActivity()
						.startActivity(i);
			}
		});
	}

	private void setupAddOnIconClickEvent(View view, Object data) {
		view.setTag(data);
		view.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (addOnIconClickListener != null) {
					addOnIconClickListener.onAddOnIconClick(v.getTag());
				}
			}
		});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.view.absadapter.ViewAdapter#getRealCount()
	 */
	@Override
	public int getRealCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

	private class ViewHolder {
		public TextView nick_name, time, call_state, delete_bt;
		public ImageView image, sex, vauth, addon_icon, call_direction;
		public View right_layout, main_layout;
	}

}
